<?php
/**
 * This file is form http://findcat.cn
 *
 * @link     https://findcat.cn
 * @email    1476982312@qq.com
 */
namespace App\Providers;

use App\Models\Article;
use App\Models\Website;
use App\Models\Category;
use App\Models\FriendlyLink;
use App\Observers\ArticleObserver;
use App\Observers\WebsiteObserver;
use Illuminate\Support\Facades\DB;
use App\Observers\CategoryObserver;
use Illuminate\Support\Facades\Log;
use App\Observers\FriendlyLinkObserver;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register()
    {
    }

    /**
     * Bootstrap any application services.
     */
    public function boot()
    {
        ////将sql写入log中
        //DB::listen(
        //    function ($query) {
        //        $tmp = str_replace('?', '"' . '%s' . '"', $query->sql);
        //        $qBindings = [];
        //        foreach ($query->bindings as $key => $value) {
        //            if (is_numeric($key)) {
        //                $qBindings[] = $value;
        //            } else {
        //                $tmp = str_replace(':' . $key, '"' . $value . '"', $tmp);
        //            }
        //        }
        //        $tmp = vsprintf($tmp, $qBindings);
        //        $tmp = str_replace('\\', '', $tmp);
        //        Log::info(' execution time: ' . $query->time . 'ms; ' . $tmp . "\n\n\t");
        //    }
        //);

        // 菜单缓存模型事件
        Category::observe(CategoryObserver::class);
        // 公告缓存模型事件
        Website::observe(WebsiteObserver::class);
        // 友情链接缓存模型事件
        FriendlyLink::observe(FriendlyLinkObserver::class);
        // 热门文章缓存模型事件
        Article::observe(ArticleObserver::class);
    }
}
